/*
 * Copyright 2016 drakeet. https://github.com/drakeet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.drakeet.multitype.sample.multiSelectable;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import me.drakeet.multitype.group.GroupInfo;
import me.drakeet.multitype.group.GroupWrapper;
import me.drakeet.multitype.sample.MenuBaseActivity;
import me.drakeet.multitype.sample.R;
import me.drakeet.multitype.sample.common.Category;
import me.drakeet.multitype.sample.common.CategoryItemViewBinder;
import me.drakeet.multitype.stick.Builder;
import me.drakeet.multitype.stick.StickHeaderControl;
import me.drakeet.multitype.stick.StickHeaderWrapper;

import static me.drakeet.multitype.MultiTypeAsserts.assertAllRegistered;

public class MultiSelectableActivity extends MenuBaseActivity {

  private static final int SPAN_COUNT = 5;
  Items items = new Items();
  MultiTypeAdapter adapter;
  Button fab;
  private TreeSet<Integer> selectedSet;

  private StickHeaderAdapter wrapperAdapter;
  private GroupWrapper groupWrapper;



  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_multi_selectable);
    RecyclerView recyclerView = findViewById(R.id.list);
    final GridLayoutManager layoutManager = new GridLayoutManager(this, SPAN_COUNT);
//    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
//      @Override
//      public int getSpanSize(int position) {
//        return (items.get(position) instanceof Category) ? SPAN_COUNT : 1;
//      }
//    });

    selectedSet = new TreeSet<>();

    recyclerView.setLayoutManager(layoutManager);
    adapter = new MultiTypeAdapter();
    adapter.register(Category.class, new CategoryItemViewBinder());
    adapter.register(Square.class, new SquareViewBinder(selectedSet));

    loadData();

    assertAllRegistered(adapter, items);

    groupWrapper=new GroupWrapper(adapter);


    recyclerView.setAdapter(groupWrapper);
    groupWrapper.setGroupList(groupInfos);

    groupWrapper.notifyDataSetChanged();
    wrapperAdapter=new StickHeaderAdapter(adapter);
   StickHeaderControl control= StickHeaderControl.getInstance(Builder.getInstance()
            .setRecyclerView(recyclerView)
            .setWrapper(wrapperAdapter))
    .goStick();

    groupWrapper.setListener(new GroupWrapper.IExpandListener() {
      @Override
      public void onExpand(RecyclerView.ViewHolder holder, Object o, int position) {
        control.goStick();
      }

      @Override
      public void onShrink(RecyclerView.ViewHolder holder, Object o, int position) {
        control.goStick();
      }
    });


    setupFAB();
  }

  public List<GroupInfo> groupInfos=new ArrayList<>();

  private void loadData() {
    GroupInfo groupInfo=new GroupInfo();
    groupInfo.children=new ArrayList();
    Category spacialCategory = new Category("特别篇");
    groupInfo.parent=spacialCategory;
    items.add(spacialCategory);
    for (int i = 0; i < 7; i++) {
      groupInfo.children.add(new Square(i + 1));
      items.add(new Square(i + 1));
    }
    groupInfos.add(groupInfo);
    GroupInfo groupInfo1=new GroupInfo();
    groupInfo1.children=new ArrayList();
    Category currentCategory = new Category("本篇");
    groupInfo1.parent=currentCategory;
    items.add(currentCategory);
    for (int i = 0; i < 1000; i++) {
      groupInfo1.children.add(new Square(i + 100));
      items.add(new Square(i + 1));
    }
    groupInfos.add(groupInfo1);
    adapter.setItems(items);
    adapter.notifyDataSetChanged();
  }


  private void setupFAB() {
    fab = findViewById(R.id.fab);
    fab.setOnClickListener(v -> {
      StringBuilder content = new StringBuilder();
      for (Integer number : selectedSet) {
        content.append(number).append(" ");
      }
      Toast.makeText(v.getContext(),
          "Selected items: " + content, Toast.LENGTH_SHORT)
          .show();
    });
  }

  public class StickHeaderAdapter extends StickHeaderWrapper{

    public StickHeaderAdapter(MultiTypeAdapter adapter) {
      super(adapter);
    }

    @Override
    public boolean isHeader(int position) {
      return adapter.getItems().get(position) instanceof Category;
    }
  }
}
